/**
 * @version 1.0.0
 * @Title: Sequence
 * @Package: lx.music.sound.midi
 * @Description: Copyright: Copyright (c) 2016
 * @Company: 成都壹柒互动科技有限公司
 * @author huangzhilong.hzl
 * @date 2017-10-30 19:07
 */
package lx.music.sound.midi;

import java.util.Vector;

/**
 * @version 1.0.0
 * @Title: Sequence
 * @Package: lx.music.sound.midi
 * @Description: Copyright: Copyright (c) 2016
 * @Company: 成都壹柒互动科技有限公司
 * @author huangzhilong.hzl
 * @date 2017-10-30 19:07
 */
public class Sequence {

    public static final float  PRQ = 0.0f ;

    public static final float SMPTE_24 = 24.0f ;

    public static final float SMPTE_25 = 25.0f ;

    public static final float SMPTE_30DROP = 29.97f ;

    public static final float SMPTE_30 = 30.0f ;

    protected float divisionType ;

    protected int resolution ;

    private Vector<Track> tracks = new Vector<Track>() ;

    public Sequence(float divisionType,int resolution)throws InvalidMidiDataException{
        if(divisionType==PRQ){
            this.divisionType = PRQ;
        }else if(divisionType==SMPTE_24){
            this.divisionType = SMPTE_24;
        }else if(divisionType==SMPTE_25){
            this.divisionType = SMPTE_25 ;
        }else if(divisionType==SMPTE_30DROP){
            this.divisionType = SMPTE_30DROP ;
        }else if(divisionType==SMPTE_30){
            this.divisionType = SMPTE_30;
        }else {
            throw new InvalidMidiDataException("Unsupported division type: "+divisionType);
        }

        this.resolution = resolution ;
    }

    public Sequence(float divisionType,int resolution, int numTracks) throws InvalidMidiDataException{
        if(divisionType==PRQ){
            this.divisionType = PRQ;
        }else if(divisionType==SMPTE_24){
            this.divisionType = SMPTE_24;
        }else if(divisionType==SMPTE_25){
            this.divisionType = SMPTE_25 ;
        }else if(divisionType==SMPTE_30DROP){
            this.divisionType = SMPTE_30DROP ;
        }else if(divisionType==SMPTE_30){
            this.divisionType = SMPTE_30;
        }else {
            throw new InvalidMidiDataException("Unsupported division type: "+divisionType);
        }

        this.resolution = resolution ;

        for(int i=0;i<numTracks;i++){
            tracks.addElement(new Track());
        }
    }

    public float getDivisionType(){
        return divisionType ;
    }

    public int getResolution(){
        return resolution ;
    }

    public Track createTrack(){
        Track track = new Track();
        tracks.addElement(track);

        return track ;
    }

    public boolean deleteTrack(Track track){
        synchronized (tracks){
            return tracks.removeElement(track);
        }
    }


    public Track[]  getTracks(){

        return (Track[]) tracks.toArray(new Track[tracks.size()]) ;
    }

    public long getMicrosecondLength() {
        //TODO sun的sound包 不兼容
//        return com.sun.media.sound.MidiUtils.tick2microsecond(this, getTickLength(), null);
        return -1;
    }

    public long getTickLength(){
        long length = 0 ;
        synchronized (tracks){
            for(int i=0 ; i<tracks.size();i++){
                long temp = ((Track)tracks.elementAt(i)).ticks();
                if(temp>length){
                    length = temp;
                }
            }
            return length ;
        }
    }

    public Patch[] getPatchList(){
        return new Patch[0] ;
    }

}
